package com.yoyosys.db.partition.query.impl;

import com.yoyosys.db.partition.constant.PartitionType;
import com.yoyosys.db.partition.query.PartitionQuery;
import com.yoyosys.db.partition.vo.Partition;
import com.yoyosys.db.partition.vo.PartitionInfo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * hana 分区分桶信息查询器
 */
public class HanaPartitionQuery implements PartitionQuery {
    private static HanaPartitionQuery instance = new HanaPartitionQuery();
    private HanaPartitionQuery(){}
    public static HanaPartitionQuery getInstance(){
        return instance;
    }

    /**
     * 查询表分区信息
     *
     * @param conn
     * @param schemaName 库名
     * @param tableName
     * @return
     */
    @Override
    public PartitionInfo getPartition(Connection conn, String schemaName, String tableName) throws SQLException {
        PartitionInfo info = new PartitionInfo();
        info.setSchemaName(schemaName);
        info.setTableName(tableName);
        info.setPartitions(new ArrayList<>());

        // 获取分区类型和键
        String partitionTypeSql = new StringBuilder()
                .append(" SELECT  ")
                .append("     t.partition_type, ")
                .append("     p.partition_column_name AS partition_key ")
                .append(" FROM tables t ")
                .append(" JOIN table_partition_columns p ON (t.table_name = p.table_name AND t.schema_name = p.schema_name) ")
                .append(" WHERE t.schema_name = ? ")
                .append(" AND t.table_name = ? ")
                .toString();
        System.out.println("partitionTypeSql: " + partitionTypeSql);
        try (PreparedStatement pstmt = conn.prepareStatement(partitionTypeSql)) {
            pstmt.setString(1, schemaName);
            pstmt.setString(2, tableName);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    info.setPartitionType(rs.getString("partition_type"));
                    info.setPartitionKey(rs.getString("partition_key"));
                } else {
                    info.setPartitionType(PartitionType.NONE.name());
                    return info;
                }
            }
        }

        // 获取所有分区
        String partitionsSql = new StringBuilder()
                .append("SELECT PARTITION_NAME, PARTITION_RANGE, ROW_COUNT ")
                //.append(", STORAGE_LOCATION, ACTIVE_STATUS ")
                .append("FROM SYS.TABLE_PARTITIONS ")
                .append("WHERE SCHEMA_NAME = ? AND TABLE_NAME = ? ")
                .append("ORDER BY PARTITION_ID")
                .toString();
        System.out.println("partitionsSql: " + partitionsSql);
        try (PreparedStatement pstmt = conn.prepareStatement(partitionsSql)) {
            pstmt.setString(1, schemaName);
            pstmt.setString(2, tableName);
            try (ResultSet rs = pstmt.executeQuery()) {
                while (rs.next()) {
                    Partition partition = new Partition();
                    partition.setName("PART_" + rs.getInt("PARTITION_NAME"));
                    partition.setExpression(rs.getString("PARTITION_RANGE"));
                    partition.setRowCount(rs.getLong("ROW_COUNT"));
                    //partition.setStorageLocation(rs.getString("STORAGE_LOCATION"));
                    //partition.setStatus(rs.getString("ACTIVE_STATUS"));
                    info.getPartitions().add(partition);
                }
            }
        }

        return info;
    }
}
